package com.longge;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-04-20 02:13
 */
public class NumComponents {

    public int numComponents(ListNode head, int[] G) {

        Set<Integer> set = IntStream.of(G).boxed().collect(Collectors.toSet());
        int i = 0;
        while (head != null) {
            Boolean b = false;
            if (set.contains(head.val)) {
                b = true;
            }
            head = head.next;
            if (b && head == null) {
                i++;
            }else if (b && !set.contains(head.val)) {
                i++;
            }
        }
        return i;
    }
}
